<template>
  <el-card :shadow="shadow" class="status-card" :class="`status-${status}`">
    <div class="card-inner">
      <div class="card-header">
        <div class="header-left">
          <h3 class="card-title">{{ title }}</h3>
          <el-tag 
            :type="statusType" 
            size="small"
            effect="light"
            class="status-badge"
          >
            {{ statusText }}
          </el-tag>
        </div>
        <div class="icon-container">
          <el-icon :size="24" :class="`status-icon status-${status}-icon`">
            <component :is="statusIcon" />
          </el-icon>
        </div>
      </div>
      
      <div class="card-body">
        <div class="value-container">
            <transition name="value-change">
             <span :key="value" class="main-value">{{ formatValue(value) }}</span>
            </transition>
            <span class="unit">{{ unit }}</span>
          </div>
        
        <div class="trend-container" v-if="trend !== undefined">
          <div 
            :class="['trend-indicator', {
              'trend-up': trend > 0,
              'trend-down': trend < 0,
              'trend-neutral': trend === 0
            }]"
          >
            <el-icon :size="16">
              <ArrowUp v-if="trend > 0" />
              <ArrowDown v-else-if="trend < 0" />
              <Minus v-else />
            </el-icon>
            <span :class="['trend-value', {
              'trend-up': trend > 0,
              'trend-down': trend < 0
            }]">
              {{ formatTrend(trend) }}%
            </span>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { ArrowUp, ArrowDown, Minus, CircleCheck, CircleClose, WarningFilled, InfoFilled } from '@element-plus/icons-vue';

// 组件参数
const props = defineProps({
  title: {
    type: String,
    required: true
  },
  value: {
    type: [Number, String],
    required: true
  },
  unit: {
    type: String,
    default: ''
  },
  status: {
    type: String,
    default: 'normal',
    validator: (value) => ['normal', 'warning', 'error', 'success'].includes(value)
  },
  trend: {
    type: Number,
    default: undefined
  },
  shadow: {
    type: String,
    default: 'hover',
    validator: (value) => ['always', 'hover', 'never'].includes(value)
  }
});

// 根据状态生成标签类型和文本
const statusType = {
  normal: 'info',
  warning: 'warning',
  error: 'danger',
  success: 'success'
}[props.status];

const statusText = {
  normal: '正常',
  warning: '警告',
  error: '错误',
  success: '良好'
}[props.status];

// 根据状态选择图标
const statusIcon = {
  normal: InfoFilled,
  warning: WarningFilled,
  error: CircleClose,
  success: CircleCheck
}[props.status];

// 格式化数值的辅助函数
const formatValue = (val) => {
  if (val === null || val === undefined || val === '--') {
    return '--';
  }
  
  // 检查是否为数字
  const num = parseFloat(val);
  if (!isNaN(num)) {
    return num.toFixed(2);
  }
  return val;
};

// 格式化趋势百分比的辅助函数
const formatTrend = (val) => {
  if (val === null || val === undefined) {
    return '0.00';
  }
  
  // 检查是否为数字
  const num = parseFloat(val);
  if (!isNaN(num)) {
    return Math.abs(num).toFixed(2);
  }
  return '0.00';
};
</script>

<style scoped>
.status-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
  overflow: hidden;
  background: white;
  position: relative;
}

.status-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background-color: #e4e7ed;
  transition: background-color 0.3s ease;
}

.status-success::before {
  background-color: #67c23a;
}

.status-warning::before {
  background-color: #e6a23c;
}

.status-error::before {
  background-color: #f56c6c;
}

.status-normal::before {
  background-color: #409eff;
}

.status-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.card-inner {
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  line-height: 1.4;
}

.status-badge {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
}

.icon-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background-color: #f5f7fa;
  transition: background-color 0.3s ease;
}

.status-card:hover .icon-container {
  background-color: #eef2ff;
}

.status-icon {
  transition: transform 0.3s ease;
}

.status-card:hover .status-icon {
  transform: scale(1.1);
}

.status-success-icon {
  color: #67c23a;
}

.status-warning-icon {
  color: #e6a23c;
}

.status-error-icon {
  color: #f56c6c;
}

.status-normal-icon {
  color: #409eff;
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.value-container {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.main-value {
  font-size: 32px;
  font-weight: 700;
  color: #303133;
  line-height: 1;
  transition: all 0.3s ease;
}

.unit {
  font-size: 16px;
  color: #909399;
  font-weight: 500;
}

.trend-container {
  display: flex;
  align-items: center;
}

.trend-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  background-color: #f5f7fa;
  transition: all 0.3s ease;
}

.trend-up {
  color: #f56c6c;
}

.trend-down {
  color: #67c23a;
}

.trend-neutral {
  color: #909399;
}

.status-card:hover .trend-indicator {
  transform: translateX(4px);
}

/* 数值变化动画 */
.value-change-enter-active,
.value-change-leave-active {
  transition: all 0.5s cubic-bezier(0.19, 1, 0.22, 1);
}

.value-change-enter-from {
  opacity: 0;
  transform: translateY(-10px);
}

.value-change-leave-to {
  opacity: 0;
  transform: translateY(10px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-inner {
    padding: 20px 16px;
  }
  
  .main-value {
    font-size: 28px;
  }
  
  .card-title {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .main-value {
    font-size: 24px;
  }
}
</style>
